<template>
  <div class="free-course">
    <dl>
      <dt>
        <span class="active">{{ tabList.title }}</span>
      </dt>
      <dd v-for="(item,index) in CourseList" :key="index" class="course-item">
        <div class="img-box">
          <img :src="item.courseCover" alt="">
        </div>
        <div class="course-content">
          <p class="title">
            {{ item.courseName }} <span class="status">{{ `更新完毕` }}</span>
          </p>
          <p class="learn">
            <span class="chapter">学习至{{ item.periodName }}</span>
          </p>
          <p class="other">
            <span style="margin-top: 15px">{{ item.gmtModified }}</span>
            <span class="learn-btn" @click="continueStudy(item)">继续学习</span>
          </p>
        </div>
      </dd>
    </dl>

    <div v-if="CourseList.length === 0 || CourseList === null" class="list-empty">
      <p>暂无学习记录</p>
    </div>

    <!-- 分页 -->
    <el-pagination
       :current-page="page"
       :page-size="limit"
       :total="total"
       style="padding: 30px 0; text-align: center;"
       layout="total, prev, pager, next, jumper"
       @current-change="fetchData"
    />
  </div>
</template>
<script>
import { getUserStudyPageList } from '@/api/study';
import { mapGetters } from 'vuex';

export default {
  data () {
    return {
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      currentTabIndex: 0, // 当前选项卡的索引
      CourseList:[], // 最近学习列表
      tabList:{ title: '最近学习', type: 0 }
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData(page = 1){
      // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
      // 解决分页无效问题
      this.page = page

      getUserStudyPageList(this.page,this.limit,this.userId).then(result => {
        let {code,data} = result;
        if (code === 20000){
          this.CourseList = data.rows
          this.total = data.total
        }
      })
    },
    continueStudy(item){
      let url = this.$router.resolve({
        path: '/course/'+item.courseId,
      });
      window.open(url.href, '_blank')
    }
  },
  computed: {
    ...mapGetters({
      'userId': 'id'
    })
  },
}
</script>
<style lang="stylus" scoped>
  .free-course
    dt
      border-bottom: 1px solid #d0d6d9;
      & > span
        margin-right: 48px;
        display: inline-block;
        vertical-align: middle;
        height: 48px;
        line-height: 48px;
        box-sizing: border-box;
        font-size: 14px;
        color: #787d82
        cursor: pointer;
        &:last-child
          margin-right: 0;
        &.active
          color: #f01414;
          border-bottom: 2px solid #f01414;
    .course-item
      display: flex;
      align-items: flex-start;
      padding: 30px 0;
      border-bottom: 1px solid #eff1f0;
      &:last-child
        border-bottom: none;
      .img-box
        flex: 0 0 200px;
        width: 200px;
        height: 113px;
        & > img
          display: block;
          width: 100%;
          height: 100%;
      .course-content
        flex: 1;
        margin-left: 30px;
        .title
          font-size: 18px;
          font-weight: 700;
          line-height: 30px;
          .status
            float: right;
            margin-right: 25px;
            font-size: 14px;
            color: #787d82;
        .learn
          padding: 10px 0 22px;
          & > span
            display: inline-block;
            vertical-align: middle;
            margin-right: 15px;
            color: #787d82;
            line-height: 20px;
            font-size: 14px;
            &:last-child
              margin-right: 0;
            &.rate
              color: #f01414;
        .other
          & > span
            margin-right: 100px;
            display: inline-block;
            vertical-align: middle;
            font-size: 14px;
            color: #787d82;
            &.learn-btn
              float: right;
              margin-right: 0;
              width: 104px;
              height: 36px;
              background-color: #f01414;
              border-radius: 18px;
              text-align: center;
              line-height: 36px;
              color: #fff;
              cursor: pointer;
  .list-empty
    margin-top 20px
    text-align: center;
</style>
